package com.taptech.doufu.app;

import android.app.Activity;

import com.taptech.doufu.ui.activity.weex.TFWXActivity;

import java.util.Stack;

public class MyActivityManager {

    private static MyActivityManager instance = new MyActivityManager();
    private Stack<Activity> activityStack = new Stack<>();

    public static MyActivityManager getInstance() {
        return instance;
    }

    public void pushActivity(Activity activity){
        if (activityStack == null){
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    public void popActivity(){
        popActivity(activityStack.lastElement());
    }

    public void popActivity(Activity activity){
        try {
            if (activity!=null){
                activity.finish();
                activityStack.remove(activity);
                activity = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String getCurPageName() {
        String page = "";
        try {
            Activity currActivity = getCurrActivity();
            if (currActivity != null) {
                if(currActivity instanceof TFWXActivity){
                    page = ((TFWXActivity)currActivity).getPageUrl();
                }else{
                    page =currActivity.getClass().getSimpleName();
                }
            }
            return page;
        } catch (Exception e) {
            return page;
        }

    }

    private Activity getCurrActivity(){
        try {
            return activityStack.lastElement();
        }catch (Exception e){
            return null;
        }
    }

    public String getParentPageName(){
        String page = "";
        try {
            Activity parentActivity = getParentActivity();
            if (parentActivity != null) {
                if(parentActivity instanceof TFWXActivity){
                    page = ((TFWXActivity)parentActivity).getPageUrl();
                }else{
                    page =parentActivity.getClass().getSimpleName();
                }
            }
            return page;
        } catch (Exception e) {
            return page;
        }

    }

    private Activity getParentActivity(){
        try {
            if (activityStack.size()>1){
                return activityStack.elementAt(activityStack.size()-2);
            }else{
                return null;
            }
        } catch (Exception e) {
            return null;
        }

    }

}
